home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / zfiledev.c < prev    next >
C/C++ Source or Header  |  1993-05-13  |  9KB  |  312 lines

  1. /* Copyright (C) 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zfiledev.c */
  20. /* File device implementation for Ghostscript */
  21. #include "stdio_.h"
  22. #include "string_.h"
  23. #include "ghost.h"
  24. #include "gp.h"
  25. #include "errors.h"
  26. #include "oper.h"
  27. #include "stream.h"
  28. #include "filedev.h"            /* must come after stream.h */
  29. #include "files.h"
  30.  
  31. extern const uint file_default_buffer_size;
  32.  
  33. /* Routines in zfile.c */
  34. extern stream *file_alloc_stream(P0());
  35. extern int file_close_disable(P1(stream *));
  36. extern int file_close_finish(P1(stream *));
  37.  
  38. /* The OS (%os%) device. */
  39. fdev_proc_open_file(fdev_os_open_file);        /* os_open */
  40. private fdev_proc_fopen(os_fopen);
  41. private fdev_proc_fclose(os_fclose);
  42. private fdev_proc_delete_file(os_delete);
  43. private fdev_proc_rename_file(os_rename);
  44. private fdev_proc_enumerate_files(os_enumerate);
  45. private const file_device fdev_os = {
  46.     "os",
  47.     { fdev_no_init, fdev_no_open_device,
  48.       fdev_os_open_file, os_fopen, os_fclose,
  49.       os_delete, os_rename,
  50.       os_enumerate, gp_enumerate_files_next, gp_enumerate_files_close
  51.     }
  52. };
  53.  
  54. /* The special devices. */
  55. #define fdev_special(dname, init, open)\
  56.   { dname,\
  57.      { init, open, fdev_no_open_file, fdev_no_fopen, fdev_no_fclose,\
  58.        fdev_no_delete_file, fdev_no_rename_file,\
  59.        fdev_no_enumerate_files\
  60.      }\
  61.   }
  62.  
  63. #define stdin_buf_size 1
  64. stream *gs_stream_stdin;    /* exported for zfileio.c only */
  65. private fdev_proc_init(stdin_init);
  66. private fdev_proc_open_device(stdin_open);
  67. private const file_device fdev_stdin =
  68.   fdev_special("stdin", stdin_init, stdin_open);
  69.  
  70. #define stdout_buf_size 128
  71. stream *gs_stream_stdout;    /* exported for zfileio.c only */
  72. private fdev_proc_init(stdout_init);
  73. private fdev_proc_open_device(stdout_open);
  74. private const file_device fdev_stdout =
  75.   fdev_special("stdout", stdout_init, stdout_open);
  76.  
  77. #define stderr_buf_size 128
  78. stream *gs_stream_stderr;    /* exported for zfileio.c only */
  79. private fdev_proc_init(stderr_init);
  80. private fdev_proc_open_device(stderr_open);
  81. private const file_device fdev_stderr =
  82.   fdev_special("stderr", stderr_init, stderr_open);
  83.  
  84. #define lineedit_buf_size 160
  85. private byte *lineedit_buf;    /****** QUESTIONABLE ******/
  86. private fdev_proc_init(lineedit_init);    /****** QUESTIONABLE ******/
  87. private fdev_proc_open_device(lineedit_open);
  88. private const file_device fdev_lineedit =
  89.   fdev_special("lineedit", lineedit_init, lineedit_open);
  90.  
  91. private fdev_proc_open_device(statementedit_open);
  92. private const file_device fdev_statementedit =
  93.   fdev_special("statementedit", fdev_no_init, statementedit_open);
  94.  
  95. /*
  96.  * The table of all known file devices.  The first entry must be fdev_os,
  97.  * since it is the default for files with no explicit device specified.
  98.  *
  99.  * The table can be extended by configurable "resource" declarations,
  100.  * like drivers, operators, etc.
  101.  */
  102. /* Declare additional file devices as extern first. */
  103. #define file_device_(fdev) extern const file_device fdev;
  104. #include "gconfig.h"
  105. #undef file_device_
  106.  
  107. const file_device *file_device_table[] = {
  108.     &fdev_os,
  109.     &fdev_stdin, &fdev_stdout, &fdev_stderr,
  110.     &fdev_lineedit, &fdev_statementedit,
  111. #define file_device_(fdev) &fdev,
  112. #include "gconfig.h"
  113. #undef file_device_
  114.     NULL
  115. };
  116.  
  117. /* ------ Initialization ------ */
  118.  
  119. private void
  120. zfiledev_init(void)
  121. {    /* Run the one-time initialization of each file device. */
  122.     const file_device **pfdev = &file_device_table[0];
  123.     for ( ; *pfdev; pfdev++ )
  124.         ((*pfdev)->procs.init)((file_device *)*pfdev);
  125. }
  126.  
  127. /* ------ Default (unimplemented) file device procedures ------ */
  128.  
  129. int
  130. fdev_no_init(file_device *fdev)
  131. {    return 0;
  132. }
  133.  
  134. int
  135. fdev_no_open_device(file_device *fdev, const char *access, stream **ps)
  136. {    return_error(e_invalidfileaccess);
  137. }
  138.  
  139. int
  140. fdev_no_open_file(file_device *fdev, const char *fname, uint namelen,
  141.   const char *access, stream **ps)
  142. {    return_error(e_invalidfileaccess);
  143. }
  144.  
  145. FILE *
  146. fdev_no_fopen(file_device *fdev, const char *fname, const char *access)
  147. {    return NULL;
  148. }
  149.  
  150. int
  151. fdev_no_fclose(file_device *fdev, FILE *file)
  152. {    return_error(e_ioerror);
  153. }
  154.  
  155. int
  156. fdev_no_delete_file(file_device *fdev, const char *fname)
  157. {    return_error(e_invalidfileaccess);
  158. }
  159.  
  160. int
  161. fdev_no_rename_file(file_device *fdev, const char *from, const char *to)
  162. {    return_error(e_invalidfileaccess);
  163. }
  164.  
  165. file_enum *
  166. fdev_no_enumerate_files(file_device *fdev, const char *pat, uint patlen,
  167.   const gs_memory_procs *mprocs)
  168. {    return NULL;
  169. }
  170.  
  171. /* ------ %os% ------ */
  172.  
  173. /* The open_file routine is exported for pipes. */
  174. int
  175. fdev_os_open_file(file_device *fdev, const char *fname, uint len,
  176.   const char *file_access, stream **ps)
  177. {    return file_open_stream(fname, len, file_access,
  178.                 file_default_buffer_size, ps,
  179.                 fdev->procs.fopen);
  180. }
  181.  
  182. private FILE *
  183. os_fopen(file_device *fdev, const char *fname, const char *access)
  184. {    return fopen(fname, access);
  185. }
  186.  
  187. private int
  188. os_fclose(file_device *fdev, FILE *file)
  189. {    fclose(file);
  190.     return 0;
  191. }
  192.  
  193. private int
  194. os_delete(file_device *fdev, const char *fname)
  195. {    return (unlink(fname) == 0 ? 0 : e_ioerror);
  196. }
  197.  
  198. private int
  199. os_rename(file_device *fdev, const char *from, const char *to)
  200. {    return (rename(from, to) == 0 ? 0 : e_ioerror);
  201. }
  202.  
  203. private file_enum *
  204. os_enumerate(file_device *fdev, const char *pat, uint patlen,
  205.   const gs_memory_procs *mprocs)
  206. {    return gp_enumerate_files_init(pat, patlen, mprocs);
  207. }
  208.  
  209. /* ------- %stdin, %stdout, and %stderr ------ */
  210.  
  211. private int
  212. stdin_init(file_device *fdev)
  213. {
  214.     /****** stdin SHOULD NOT LINE-BUFFER ******/
  215.  
  216.     byte *buf = (byte *)gs_malloc(stdin_buf_size, 1, "stdin_init");
  217.     gs_stream_stdin = (stream *)gs_malloc(1, sizeof(stream), "stdin_init");
  218.     sread_file(gs_stream_stdin, gs_stdin, buf, stdin_buf_size);
  219.     s_init_read_id(gs_stream_stdin);
  220.     gs_stream_stdin->procs.close = file_close_finish;
  221.     return 0;
  222. }
  223.  
  224. private int
  225. stdin_open(file_device *fdev, const char *access, stream **ps)
  226. {    if ( strcmp(access, "r") )
  227.         return_error(e_invalidfileaccess);
  228.     *ps = gs_stream_stdin;
  229.     return 0;
  230. }
  231.  
  232. private int
  233. stdout_init(file_device *fdev)
  234. {    byte *buf = (byte *)gs_malloc(stdout_buf_size, 1, "stdout_init");
  235.     gs_stream_stdout = (stream *)gs_malloc(1, sizeof(stream), "stdout_init");
  236.     swrite_file(gs_stream_stdout, gs_stdout, buf, stdout_buf_size);
  237.     s_init_write_id(gs_stream_stdout);
  238.     gs_stream_stdout->procs.close = file_close_finish;
  239.     return 0;
  240. }
  241.  
  242. private int
  243. stdout_open(file_device *fdev, const char *access, stream **ps)
  244. {    if ( strcmp(access, "w") )
  245.         return_error(e_invalidfileaccess);
  246.     *ps = gs_stream_stdout;
  247.     return 0;
  248. }
  249.  
  250. private int
  251. stderr_init(file_device *fdev)
  252. {    byte *buf = (byte *)gs_malloc(stderr_buf_size, 1, "stderr_init");
  253.     gs_stream_stderr = (stream *)gs_malloc(1, sizeof(stream), "stderr_init");
  254.     swrite_file(gs_stream_stderr, gs_stderr, buf, stderr_buf_size);
  255.     s_init_write_id(gs_stream_stderr);
  256.     gs_stream_stderr->procs.close = file_close_finish;
  257.     return 0;
  258. }
  259.  
  260. private int
  261. stderr_open(file_device *fdev, const char *access, stream **ps)
  262. {    if ( strcmp(access, "w") )
  263.         return_error(e_invalidfileaccess);
  264.     *ps = gs_stream_stderr;
  265.     return 0;
  266. }
  267.  
  268. /* ------ %lineedit and %statementedit ------ */
  269.  
  270. private int
  271. lineedit_init(file_device *fdev)
  272. {
  273.     /****** QUESTIONABLE ******/
  274.  
  275.     lineedit_buf =
  276.         (byte *)gs_malloc(lineedit_buf_size, 1, "lineedit_init");
  277.     return 0;
  278. }
  279.  
  280. private int
  281. lineedit_open(file_device *fdev, const char *access, stream **ps)
  282. {    uint count;
  283.     int code;
  284.     stream *s;
  285.     if ( strcmp(access, "r") )
  286.         return_error(e_invalidfileaccess);
  287.     s = file_alloc_stream();
  288.     if ( s == 0 )
  289.         return_error(e_VMerror);
  290.     code = zreadline_from(lineedit_buf, lineedit_buf_size, &count,
  291.                   gs_stream_stdin);
  292.     if ( code != 1 )
  293.         return (code < 0 ? code : e_undefinedfilename /* EOF */);
  294.     sread_string(s, lineedit_buf, count);
  295.     s->save_close = s->procs.close;
  296.     s->procs.close = file_close_disable;
  297.     *ps = s;
  298.     return 0;
  299. }
  300.  
  301. private int
  302. statementedit_open(file_device *fdev, const char *access, stream **ps)
  303. {    /* NOT IMPLEMENTED PROPERLY YET */
  304.     return lineedit_open(fdev, access, ps);
  305. }
  306.  
  307. /* ------ Initialization procedure ------ */
  308.  
  309. op_def zfiledev_op_defs[] = {
  310.     op_def_end(zfiledev_init)
  311. };
  312.